﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using BusinessComponents;

namespace PresentationLayerWebForms
{
    public partial class GuestsInformation : ReservationBaseController
    {
        protected override void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            currentPage = "GuestsInformation.aspx";

            Booking booking = (Booking)Session["Booking"];
            if (booking == null || booking.RoomTypeID == 0)
            {
                this.FirstPage();
            }

            lblArrivalDate.Text = ((DateTime)booking.StartDate).ToString("dddd, MMMM dd, yyyy");
            lblDepartureDate.Text = ((DateTime)booking.EndDate).ToString("dddd, MMMM dd, yyyy");
            double noOfNights = ((DateTime)booking.EndDate).Subtract((DateTime)booking.StartDate).TotalDays;
            lblNight.Text = noOfNights.ToString();
            
            lblRoom.Text = booking.NoOfRoom.ToString();


            lblRoomType.Text = booking.RoomType.RoomTypeName;
            lblRoomCharges.Text = booking.RoomType.Price.ToString();
            double total = booking.RoomType.Price.Value * booking.NoOfRoom.Value * noOfNights;

            double discount = 0;
            Session["totalAmount"] = total;

            try
            {
                if (booking.Payment.Voucher != null)
                {
                    discount = (double)booking.Payment.Voucher.Amount;
                }
            }
            catch (NullReferenceException excep)
            {
                Console.WriteLine(excep.Message);
            }
            lblDiscount.Text = discount.ToString();
            lblTotalRate.Text = (total - discount).ToString();
        }

        protected void btnBookReservation_Click(object sender, EventArgs e)
        {
            Booking booking = (Booking)Session["Booking"];

            Guest guest = new Guest();
            guest.Email = txtEmailAddress.Text;
            guest.FirstName = txtFirstName.Text;
            guest.LastName = txtLastName.Text;
            guest.Phone = txtPhoneNumber.Text;
            guest.City = txtCity.Text;
            guest.ZipPostalCode = txtPostalCode.Text;
            guest.Country = txtCountry.Text;
            guest.CVVNumber = txtSecurityCode.Text;
            guest.CreditNumber = txtCreditCardNumber.Text;
            guest.ExpirationDate = Convert.ToDateTime(txtExpiryDate.Text);
            booking.Guests.Add(guest);

            if (!string.IsNullOrEmpty(txtGuestFirstName.Text) && !string.IsNullOrEmpty(txtGuestLastName.Text))
            {
                Guest secondGuest = new Guest();
                secondGuest.FirstName = txtGuestFirstName.Text;
                secondGuest.LastName = txtLastName.Text;
                booking.Guests.Add(secondGuest);
            }

            Payment payment = new Payment();
            payment.PaymentType = Convert.ToInt32(ddlPaymentType.SelectedIndex);
            payment.Amount = Convert.ToInt32(Session["totalAmount"]);
            booking.Payment = payment;

            string bookingID = businessComponentsFacade.MakeReservation(booking);
            if (string.IsNullOrEmpty(bookingID))
            {
                lblError.Text = businessComponentsFacade.ErrorMessage;
            }
            else
            {
                Session["BookingID"] = bookingID;
                Response.Redirect("Thanks.aspx");
            }
        }
    }
}